<?php

/*
 * Author: Quan Van Sinh
 * Email : qvsinh@gmail.com
 * Mobile: 0972405165
 */

class stream {

    function stream() {
        
    }

    function listComment($limit=20, $share_from) {
        global $db;
        $mid = intval($_GET['mid']);
        $sqlGeo = "SELECT * FROM member WHERE id=$mid";
        $arrGeo = $db->QueryFirst($sqlGeo);
        $geo_id = intval($arrGeo['geo_id']);
        $sql = "SELECT * FROM stream WHERE 1 AND parent_id=0";
        if ($share_from != "") {
            $sql.=" AND (mid=$mid or mid in (select friend_id from friend where groups like '%|$share_from|%'))";
        } else {
            $sql.=" AND (mid=$mid or mid in (select id from member where geo_id=$geo_id))";
        }
        $sql.=" ORDER BY id DESC LIMIT $limit";
        if ($mid > 0) {
            $rs = $db->query($sql);
        }
        return array('rs' => $rs);
    }

    function listComment2($limit=20, $share_from) {
        global $db;
        $arrGroups = array();
        $mid = intval($_GET['mid']);
        $sqlG = "Select groups from friend where friend_id=$mid";
        $rsG = $db->query($sqlG);
        while ($row = $db->FetchToArray($rsG)) {
            $arrGroups[] = $row["groups"];
        }
        $strGroups = implode($arrGroups);
        $arrGroups = explode("|", $strGroups);

        $sql = "SELECT * FROM stream WHERE 1 AND parent_id=0 AND (mid=$mid ";
        $share = "";
        foreach ($arrGroups as $key => $value) {
            if ($value != "") {
                $share.="or LOCATE('|" . $value . "|', share_for)>0";
            }
        }
        $sql.=$share . ")";
        $sql.=" ORDER BY id DESC LIMIT $limit";
        if ($mid > 0) {
            $rs = $db->query($sql);
        }
        return array('rs' => $rs);
    }

    function listFriendStream($filter, $last_id=0, $limit=20) {
        global $db;
        $arrGroups = array();
        $mid = intval($_GET['mid']);
        $sqlG = "Select groups from friend where friend_id=$mid ";
        if ($filter != "fr")
            $sqlG.=" and mid in (SELECT friend_id FROM friend WHERE mid={$mid} AND groups like '%|$filter|%')";

        $rsG = $db->query($sqlG);
        while ($row = $db->FetchToArray($rsG)) {
            $arrGroups[] = $row["groups"];
        }
        $strGroups = implode($arrGroups);
        $arrGroups = explode(",", $strGroups);

        $sql = "SELECT * FROM stream WHERE 1 AND parent_id=0";
        $where = "";
        if (count($arrGroups) > 0) {
            foreach ($arrGroups as $key => $value) {
                if ($value != "") {
                    $where.="LOCATE('," . $value . ",', share_for)>0";
                    if ($key < count($arrGroups) - 2)
                        $where.=" or ";
                }
            }
        }
        if ($where != "")
            $where = " AND " . $where;
        else
            $where .=" AND 0>1";
        if ($last_id > 0)
            $sql.=" AND id<$last_id";
        $sql.=$where . " ORDER BY id DESC LIMIT $limit";

        if ($mid > 0) {
            $rs = $db->query($sql);
        }
       
        return array('rs' => $rs);
    }

    function listMyStream($last_id=0, $limit=20) {
        global $db;
        $mid = intval($_GET['mid']);

        if ($mid) {
            $page = intval($_GET['page']);

            if ($page < 1) {
                $page = 1;
            }

            $sql = "SELECT count(*) as numrows FROM stream WHERE mid=$mid AND parent_id=0";
            $arrTotalRows = $db->QueryFirst($sql);
            $totalPages = ceil($arrTotalRows['numrows'] / $limit);
            $start = (($page * $limit) - $limit);

            $sql = "SELECT * FROM stream WHERE mid=$mid AND parent_id=0";
            if ($last_id > 0)
                $sql.=" AND id<$last_id";
            $sql.=" order by id desc LIMIT $start,$limit";
            $rs = $db->query($sql);
            return array('rs' => $rs, 'page' => $page, 'totalPage' => $totalPages);
        } else {
            return false;
        }
    }

    function listCommentFromLastId($limit=20) {
        global $db;
        $last_id = intval($_POST['last_id']);
        $mid = intval($_POST['mid']);
        $sqlGeo = "SELECT * FROM member WHERE id=$mid";
        $arrGeo = $db->QueryFirst($sqlGeo);
        $geo_id = intval($arrGeo['geo_id']);
        $sql = "SELECT * FROM stream WHERE 1 AND parent_id=0 AND id<$last_id AND (mid=$mid ) ORDER BY id DESC LIMIT $limit";
        if ($mid > 0) {
            $rs = $db->query($sql);
        }
        return array('rs' => $rs);
    }

    function listChildComment($parent_id, $limit=20) {
        global $db;

        $sql = "SELECT * FROM stream WHERE 1 AND parent_id=$parent_id ORDER BY id ASC LIMIT $limit";
        if ($parent_id > 0) {
            $rs = $db->query($sql);
            return array('rs' => $rs);
        } else {
            return FALSE;
        }
    }

    function likeStream($mid, $stream_id) {
        global $db;

        $rsCheck = $db->query("SELECT * FROM stream WHERE id=$stream_id AND likes like '%|" . $mid . "|%'");

        if ($db->NumRows($rsCheck) <= 0) {
            $sql = "UPDATE stream SET likes=concat(likes,'" . $mid . "|') WHERE id=$stream_id";
        } else {
            $sql = "UPDATE stream SET likes=replace(likes,'" . $mid . "|','') WHERE id=$stream_id";
        }

        if ($db->query($sql))
            return true;
        else
            return false;
    }

    function listStream($limit=20, $share_for) {

        global $db;
        $sql = "SELECT friend_id from friend where 1 ";
        $arrGroup = explode("|", $groups);
        for ($i = 0; $i < count($arrGroup); $i++) {
            if (count($arrGroup) == 1) {
                $sql.="AND (groups like '%|" . $group_id . "|%')";
            } else {
                if ($i == 0) {
                    $sql.="AND (groups like '%|" . $group_id . "|%'";
                } else if ($i == count($arrGroup) - 1) {
                    $sql.=" or groups like '%|" . $group_id . "|%')";
                } else {
                    $sql.=" or groups like '%|" . $group_id . "|%'";
                }
            }
        }
        $sql.=" order by friend_id";
        $rs = $db->query($sql);
    }

}

?>
